package com.company;

public class A_12DoubleSelect {  //二分查找法

    /*
    * 二分查找发就是待查找的数和数列中的中间数比较
    * 小于就往左边找  大于则往右边找
    * 每次查找都分为两边  单独求中间值
    *
    *
    *
    * */
    public static void main(String[] args) {
        int[] nums = {1, 2, 2, 2, 4, 5, 5, 6, 7, 8, 9, 43};   // 必须是有序的数列  里面查找某个数的index  下标
        int start = 0;
        int end = nums.length;
        int key = 43;
        int a = -1 ;
        while(start <= end){
            int middle = (start + end )>>1;
//            System.out.println(middle);
            if (key < nums[middle]){
                end = middle-1;
            }else if(key > nums[middle]){
                start = middle +1;
            }else{
                a = middle;
                break;
            }


        }
        System.out.println(a);



    }
}
